package com.example.yyjc.config;

import com.example.yyjc.util.ResultBean;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName MyHanderInterceotor
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020
 **/
public class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if ((request).getMethod().toUpperCase().equals("OPTIONS")) {
            return true;
        }
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        response.setContentType("text/html;charset=utf-8");
        if(token==null || token.equals("")){
            response.getWriter().print(ResultBean.error("无token,请先登录"));
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
